﻿namespace LocationByPoint
{
    using System;
    using System.Device.Location;
    using System.Reactive.Linq;
    using System.Windows;
    using Microsoft.Phone.Controls;
    using WP7Contrib.Services.BingMaps;

    public partial class MainPage : PhoneApplicationPage
    {
        private readonly IBingMapsService bingMapsService;

        public MainPage()
        {
            InitializeComponent();

            bingMapsService = new BingMapsService("Your credentials id", "Your app id");
        }

        private void locationByPointClick(object sender, RoutedEventArgs e)
        {
            var geoCoordinate = new GeoCoordinate(Convert.ToDouble(this.lat.Text), Convert.ToDouble(this.@long.Text));
            var criterion = CriterionFactory.CreateLocationSearchForPoint(geoCoordinate);

            this.bingMapsService.SearchForLocationUsingPoint(criterion)
                .ObserveOnDispatcher()
                .Subscribe(result =>
                {
                    this.address.Text = result.Locations[0].Address.Locality;
                    this.address.Text += Environment.NewLine;
                    this.address.Text += result.Locations[0].Address.PostalCode;
                    this.address.Text += Environment.NewLine;
                    this.address.Text += result.Locations[0].Address.AdminDistrict;
                    this.address.Text += Environment.NewLine;
                    this.address.Text += result.Locations[0].Address.CountryRegion;
                });
        }
    }
}